package com.lmonkey.servlet.cart;

import com.lmonkey.dao.Basedao;
import com.lmonkey.servic.PRODUCTDao;
import com.lmonkey.shop.PRODUCT;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/admin/admin_docartdel")
public class AdminCartDel extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //连接数据库
        Connection conn = Basedao.getconn();
        //获取数据
        String b[] = request.getParameterValues("id[]");
        PreparedStatement ps = null;
        try{
            String sql = "delete from lmonkey_order where id = ?";
            for (int i = 0;i<b.length;i++){
                ps = conn.prepareStatement(sql);
                ps.setInt(1,Integer.parseInt(b[i]));
                ps.executeUpdate();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        response.sendRedirect("/lyq/admin/admin_cartselect");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        //获取数据
        String id = request.getParameter("id");
        //连接数据库
        Connection conn = Basedao.getconn();
        PreparedStatement ps =null;
        try {
            String sql = "delete from lmonkey_order where id = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,Integer.parseInt(id));
            ps.executeUpdate();
        }catch (SQLException e) {
            e.printStackTrace();
        }
        response.sendRedirect("/lyq/admin/admin_cartselect?cp="+request.getParameter("cp")+"&keywords="+request.getParameter("keywords"));
    }
}
